package com.person.jc.service.buy;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.common.Constants;
import com.person.jc.common.SQLSpell;
import com.person.jc.entity.TsUser;
import com.person.jc.service.Service;
import com.person.jc.service.base.device.BdDevicePartsService;
import com.person.jc.utils.DateUtils;
import com.person.jc.utils.SessionKit;
import com.person.jc.utils.Utils;

public class BsPartsOutlogService extends Service {
	
	public final static BsPartsOutlogService me = new BsPartsOutlogService();
	
	public List<Record> getList(Map<String, Object> map){
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT\n" );
		sql.append("	rl.order_id,\n" );
		sql.append("	cl.opt_user service_user,\n" );
		sql.append("	rl.cust_id,\n" );
		sql.append("	ta.leader_id,\n" );
		sql.append("	count(rl.opt_user) num1,\n" );
		sql.append("	IFNULL(pc.num1, 0) pnum1,\n" );
		sql.append("	IFNULL(pc.num2, 0) pnum2,\n" );
		sql.append("	("+SQLSpell.userName("cl.opt_user")+") service_user_,\n" );
		sql.append("	("+SQLSpell.customerName("rl.cust_id")+") cust_id_,\n" );
		sql.append("	("+SQLSpell.userName("ta.leader_id")+") leader_id_\n" );
		sql.append("FROM bs_repair_confirm_log cl\n" );
		sql.append("LEFT JOIN bs_repair_reg_log rl ON cl.log_id = rl.log_id\n" );
		sql.append("left join bs_task_assign ta on rl.order_id = ta.order_id and ta.order_type = 2\n" );
		sql.append("LEFT JOIN (");
		sql.append("select t2.order_id, count(t1.parts_id) num1, SUM(IF(t.parts_status = 2, 1, 0)) num2 from bd_device_parts t\n");
		sql.append("  left join bs_repair_check_detail t1 on t.parts_id = t1.parts_id\n" );
		sql.append("  left join bs_repair_reg_log t2 on t1.log_id = t2.log_id\n" );
		sql.append("	left join bs_repair_confirm_log t3 on t1.log_id = t3.log_id\n" );
		sql.append("	left join bs_parts_confirm_log t4 on t1.log_id = t4.log_id\n" );
		sql.append("where t3.confirm_status = 1 and t4.confirm_status = 1 GROUP BY t2.order_id");
		sql.append(") pc ON pc.order_id = rl.order_id\n" );
		sql.append("LEFT JOIN bs_order o on rl.order_id = o.order_id\n" );
		sql.append("WHERE\n" );
		sql.append("	cl.confirm_status = 1\n" );
		String temp = "";
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("custId")))){
			sql.append("and rl.cust_id  = ?\n" );
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("orderId")))){
			sql.append("and rl.order_id like ?\n" );
			params.add("%" + temp + "%");
		}
		sql.append("GROUP BY\n" );
		sql.append("	rl.order_id\n" );
		return Db.find(sql.toString(), Utils.getObjParam(params)); 
	}
	

	public List<Record> getViewList(Map<String, Object> map) {
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT\n");
		sql.append("	oe.*, ("+SQLSpell.userName("rl.opt_user")+") repair_user, rd.*,(SELECT pc.confirm_status from bs_parts_confirm_log pc where pc.log_id = rl.log_id) pc_confirm,\n");
		sql.append("	(select kind_name from ts_code_kind_data where kind_code = oe.device_type and type_code = 'device_type') device_type_,\n");
		sql.append("	(SELECT group_concat(t.device_name) from bd_device_info t where FIND_IN_SET(t.device_id, oe.gas_type)) gas_type_\n");
		sql.append("FROM bs_repair_confirm_log cl\n");
		sql.append("LEFT JOIN bs_repair_reg_log rl ON cl.log_id = rl.log_id\n");
		sql.append("left join bs_order_equipment oe on  rl.equ_id = oe.equ_id\n");
		sql.append("left join\n");
		sql.append("(select d.log_id, group_concat(distinct f.fault_name order by f.fault_name asc separator '; ') fault_name,group_concat(DISTINCT concat(p.parts_name, '[' , p.parts_code, ']') ORDER BY p.parts_name ASC, p.parts_code ASC separator '; ') parts_name  from bs_repair_check_detail d left join bd_fault_info f on d.fault_id = f.fault_id left join bd_device_parts p on d.parts_id = p.parts_id group by d.log_id ) rd on rd.log_id = rl.log_id\n");
		sql.append("WHERE cl.confirm_status = 1\n");
		String temp = "";
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("custId")))){
			sql.append("and rl.cust_id  = ?\n" );
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("optUser")))){
			sql.append("AND rl.opt_user = ?\n");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("serviceId")))){
			sql.append("AND cl.opt_user = ?");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("orderId")))){
			sql.append("and rl.order_id = ?\n" );
			params.add(temp);
		}
		return Db.find(sql.toString(), Utils.getObjParam(params)); 
	}
	

	public List<Record> getViewParts(String logId, String orderId) {
		List<Object> params = new ArrayList<Object>();
		String sql = "select t.*,(select c.cust_name from bd_customer c where c.cust_id = t4.cust_id) cust_name,\n" +
				"	(select oe.device_no from bs_order_equipment oe where oe.equ_id = t4.equ_id) device_no \n" +
				"  from bd_device_parts t\n" +
				"  left join bs_repair_check_detail t1 on t.parts_id = t1.parts_id\n" +
				"	left join bs_repair_confirm_log t2 on t1.log_id = t2.log_id\n" +
				"	left join bs_parts_confirm_log t3 on t1.log_id = t3.log_id\n" +
				"	left join bs_repair_reg_log t4 on t1.log_id = t4.log_id\n" +
				" where t2.confirm_status = 1 and t3.confirm_status = 1 \n ";
		if(Utils.isNotEmpty(logId)){
			sql += "and t1.log_id = ?\n" ;
			params.add(logId);
		}
		if(Utils.isNotEmpty(orderId)){
			sql += "and t4.order_id = ?\n" ;
			params.add(orderId);
		}
		return Db.find(sql, Utils.getObjParam(params)); 
	}
	
	public void saveOutlog(String orderId) {
		List<Record> list = this.getViewParts("", orderId);
		if(list!=null && list.size()>0){
			TsUser tsUser = (TsUser)SessionKit.get().getAttribute(Constants.LOGIN_USER);
			for (Record record : list) {
				String status = String.valueOf(record.get("parts_status"));
				if(!"2".equals(status)){
					Record outRecord = new Record();
					outRecord.set("parts_id_", record.get("parts_id"));
					outRecord.set("use_time", DateUtils.getDateTime());
					outRecord.set("use_by", tsUser.getUserId());
					outRecord.set("use_to", record.get("cust_name")+"-"+record.get("device_no"));
					Db.save("bd_device_parts_outlog", outRecord);
					BdDevicePartsService.me.update(new String[]{record.get("parts_id").toString()}, 2);
					super.setCRUD("设备出库", "C", "新增出库记录，设备编号：" + record.get("parts_id"));
				}
			}
		}
		
	}

}
